/**
 * 
 * Clase que representa un contrato en la plataforma. 
 * 
 * */
package com.latinia.limsp.test;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author 
 * @version 2.0
 * */
public class Contract {
	private int positionStatus;
	private int numMsgs[];
	private Status lStatus;
	private String maxChannels;
	private int counters[];
	private int numberOfMessages;
	public void setNumberOfMessages(int numberOfMessages) {
		this.numberOfMessages = numberOfMessages;		
	}
	public int[] getCounters() {
		return counters;
	}
	public void setCounters1(int[] counters) {
		this.counters = counters;
	}
	public String getMaxChannels() {
		return maxChannels;
	}
	public String getChannels() {
		return channels;
	}
	public void setMaxChannels(String maxChannels) {
		this.maxChannels = maxChannels;
	}
	private String channels;
	public String[] getChannelsArray() {
		
		ArrayList<String> emails = new ArrayList<String>();
		if(channels!=null){
			for (int i = 0; i < channels.length(); i++) {
				int coma = channels.indexOf(",");
				if (coma != -1) {
					String correo = channels.substring(0, coma);
					channels = channels.substring(coma + 1, channels.length());
					emails.add(correo);
				} else
					break;
			}
			emails.add(channels);
			String[] channelsArray= new String[emails.size()];
			int j=0;
			for(String c:emails){
				channelsArray[j]=c;j++;
			}
			return channelsArray;
		}
		
		return null;
	}

	private String typeAdaptor;
	private int i,j;
	private int contadorDestination;
	private Adaptors adaptors;
	private String ref="";
	private String id="";
	private String adaptor="";
	private String passwdProduct="";
	private String refProduct;
	private int numMsg=-1;
	public int countMsg=0;
	private String msgtypes;
	private String company;
	private String template;
	private List<Destination> destination;
	private String test_case;
	Properties properties;
	private String messageInot;
	public String getMessageInot() {
		return messageInot;
	}
	public String getTest_case() {
		return test_case;
	}
	public void setTest_case(String test_case) {
		this.test_case = test_case;
	}
	public Contract()
	{
		properties= new Properties();		    	
		i=0;
		j=0;
		contadorDestination=0;
	}
	public String getCompany() {
		return company;
	}
	public void setCompany(String company) {
		this.company = company;
	}
	public void setMsgtypes(String msgtypes) {
		this.msgtypes = msgtypes;
	}
	
	public String getRef() {
		return ref;
	}
	public int getNumMsg() {
		return numMsg;
	}
	public void setNumMsg(int num_msg) {
		this.numMsg = num_msg;
	}
	public void setRef(String ref) {
		this.ref = ref;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void setAdaptor(String adaptor) {
		this.adaptor = adaptor;
	}
	public String getPasswdProduct() {
		return passwdProduct;
	}
	public void setPasswdProduct(String passwdProduct) {
		this.passwdProduct = passwdProduct;
	}
	public String getRefProduct() {
		return refProduct;
	}
	public void setRefProduct(String refProduct) {
		this.refProduct=refProduct;
	}
	public String getMsgtypes() {
		return msgtypes;
	}
	public String getTemplate() {
		return template;
	}
	public void setTemplate(String template){
		this.template=template;
	}
	public Adaptors getAdaptors() {
		return adaptors;
	}
	public void setAdaptors(Adaptors adaptors) {
		this.adaptors = adaptors;
	}
	public String getAdaptor()
	{
		return adaptor;
	}
	public String getTypeAdaptor(){
		typeAdaptor="";
		if(adaptors.getJmsAdaptors()!=null&&adaptors.getJmsAdaptors().size()!=0&&i==0){							
			adaptor=adaptors.getJmsAdaptors().get(j);
			properties.setProperty("queueName",adaptor);
			typeAdaptor="jms";
			j++;
			if(j==adaptors.getJmsAdaptors().size()){
				j=0;
				i=1;
			}		
			return typeAdaptor;	
		}else i=1;
		if(adaptors.getWsAdaptors()!=null&&adaptors.getWsAdaptors().size()!=0&&i==1){
			adaptor=adaptors.getWsAdaptors().get(j);
			properties.setProperty("queueName",getAdaptor());
			typeAdaptor="ws";j++;
			if(j==adaptors.getWsAdaptors().size()){
				j=0;
				i=0;
			}
			return typeAdaptor;
		}
		else i=0;
		return typeAdaptor;
	}
	public void setTypeAdaptor(String typeAdaptor) {
		this.typeAdaptor = typeAdaptor;
	}
	public Destination getDestination() {
		if(destination.size()==1)
			return destination.get(0);
		else if(contadorDestination==destination.size()){
			contadorDestination=0;
		}
		Destination d=destination.get(contadorDestination);
		contadorDestination++;
		return d;
	}
	public List<Destination> getDestinations(){
		return destination;
	}

	public void setDestination(List<Destination> destination) {
		this.destination=destination;
	}
	public Properties getProperties() {		
    	properties.setProperty("factoryinitial",getAdaptors().getFactoryInitial());
    	if(getAdaptors().getProviderUrl()!=null)
    	properties.setProperty("providerUrl",getAdaptors().getProviderUrl());
    	if(getRefProduct()!=null)
    	properties.setProperty("login",getRefProduct());
    	if(getPasswdProduct()!=null)
    	properties.setProperty("password",getPasswdProduct());
    	if(getAdaptors().getQueueFactory()!=null)
    	properties.setProperty("queueFactoryName",getAdaptors().getQueueFactory());
    	if(getMsgtypes()!=null)
    	properties.setProperty("msgtypes",getMsgtypes());
		return properties;
	}
	public void setMessageInot(String messageInot) {
		this.messageInot=messageInot;		
	}
	public void setChannels(String channels) {
		this.channels=channels;		
	}
	public void setStatus(Status lStatus) {
		this.lStatus=lStatus;
		boolean isThereMsg=false;
		numMsgs=new int[lStatus.getList_def().size()];
		counters= new int[numMsgs.length];
		for (int i = 0; i < numMsgs.length; i++) {
			numMsgs[i]=(int)(Double.parseDouble(lStatus.getPercentage().get(i))/100*numberOfMessages);
			if(numMsgs[i]>0){
				isThereMsg=true;
			}
		}
		if(!isThereMsg&&numMsgs.length>=1){
			numMsgs[0]=1;
		}
	}
	public String getState(){
		String status=null;
		if(counters!=null){
			if(numberOfMessages!=0){						
				int index=positionStatus;
				while(status==null){
					if(counters[positionStatus]<numMsgs[positionStatus]){
						counters[positionStatus]++;
						status=lStatus.getList_def().get(positionStatus);					
					}
					positionStatus++;
					if(positionStatus>=numMsgs.length){
						positionStatus=0;
					}
					if(index==positionStatus&&status==null){
						for (int i = 0; i < counters.length; i++) {
							counters[i]=0;
						}
					}
				}					
			}
		}
		
		return status;
	}

}
